<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>模块管理</cite></a>
    </div>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="layuiadmin-table" lay-filter="layuiadmin-table"></table>
        </div>
        <script type="text/html" id="version">
            {{ d.version }}
            {{# if(d.have_update == 1){ }}
                <span lay-event="new" data-upgrade="{{ d.upgrade }}" style="color: white;background: red;border-radius:50%;padding:2px;font-size:12px;">New</span>
            {{# } }}
        </script>
        <script type="text/html" id="layuiadmin-app-btns">
            {{#  if(d.have_update == 1 && d.install == 1 && d.uninstall == 1){ }}<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="upgrade"><i
                    class="layui-icon layui-icon-edit"></i>升级</a>
            {{# } }}
            {{#  if(d.state == 0 && d.install == 1 && d.uninstall == 1){ }}<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="install"><i
                    class="layui-icon layui-icon-edit"></i>安装</a>
            {{# } }}
            {{#  if(d.state == 1 && d.install == 1 && d.uninstall == 1){ }}
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="uninstall"><i
                    class="layui-icon layui-icon-delete"></i>卸载</a>
            {{# } }}
        </script>
    </div>
</div>
<script>
    layui.use(['admin', 'table', 'helper', 'baseTable'], function () {
        var $           = layui.$
            , admin     = layui.admin
            , setter    = layui.setter
            , view      = layui.view
            , table     = layui.table
            , helper    = layui.helper
            , baseTable = layui.baseTable
            , form      = layui.form;
        var config      = {
            title         : '模块管理'
            , requestUrl  : '/Modules/index'
            , tableCols   : [{
                field  : 'id'
                , width: 80
                , title: 'ID'
            }, {
                field     : 'name'
                , title   : '模块名'
                , width: 120
            }, {
                field     : 'desc'
                , title   : '模块描述'
            }, {
                field     : 'version'
                , title   : '模块版本'
                , width : 120
                , templet : '#version'
            }, {
                field     : 'author'
                , title   : '模块作者'
                , width : 130 ,
            }, {
                field     : 'stateFormat'
                , title   : '状态'
                , width : 100 ,
            }, {
                title    : '操作'
                , width  : 200
                , align  : 'center'
                , fixed  : 'right'
                , toolbar: '#layuiadmin-app-btns'
            }]
            , areaWidth   : 450
            , areaHeight  : 200
            , search      : true
            , dateFilter  : true
            , tableEvent: function (event, obj) {
                window.errorAlertType = 'alert';
                var name = obj.data.name;
                let titles = {install : '安装' , uninstall : '卸载' , upgrade : '升级'};
                let title = '确定要' + titles[event] + '吗？';
                let param = {name : name};
                switch (event) {
                    case 'new' :
                        if(obj.data.upgrade != null && obj.data.upgrade.length > 0) helper.confirm(obj.data.upgrade);
                        return;
                    case 'upgrade' :
                        title = '确定要升级到' + obj.data.version + '版本吗？';
                        param.version = obj.data.version;
                        break;
                }
                helper.confirmReload(title , '/Modules/' + event , param);
            }
        };
        baseTable.render(config);
    });
</script>